package com.whfc.common.third.zobao;

import com.whfc.common.third.zobao.entity.*;
import com.whfc.common.util.HttpUtil;
import com.whfc.common.util.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: ZOBAO广播系统API
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-11-15 17:46
 */
public class ZobaoApi {

    private static final Logger logger = LoggerFactory.getLogger(ZobaoApi.class);
    private static final String HOST = "https://api.zobao.net";
    private static final String SIGN_IN = "/controller/web/sign/sign_in.php";
    private static final String READ_DEVICE_ALL = "/controller/web/deController/read_device_all_set.php";
    private static final String ADD_MUSIC = "/controller/web/music/add_music.php";
    private static final String SEARCH_MUSIC = "/controller/web/music/search_music_all_set.php";

    private static final String SUCESS = "200";

    /**
     * 访问域名
     */
    private String host;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String userpwd;

    public ZobaoApi(String host, String username, String userpwd) {
        this.host = host;
        this.username = username;
        this.userpwd = userpwd;
    }

    public ZobaoApi(String username, String userpwd) {
        this.host = HOST;
        this.username = username;
        this.userpwd = userpwd;
    }

    /**
     * 登录
     *
     * @return
     */
    public SigninData signIn() {
        Map<String, String> params = new HashMap<>(2);
        params.put("user_name", username);
        params.put("user_pwd", userpwd);
        String url = host + SIGN_IN;
        String response = HttpUtil.doPost(url, params);
        SigninResult result = JSONUtil.parseObject(response, SigninResult.class);
        if (result != null && SUCESS.equals(result.getCode())) {
            return result.getDatas();
        }
        return null;
    }

    /**
     * 添加音源
     *
     * @param userid
     * @param project_id
     * @param region_id
     * @param name
     * @param file
     */
    public void addMusic(String userid, String project_id, String region_id, String name, File file) {
        Map<String, String> params = new HashMap<>(4);
        params.put("userid", userid);
        params.put("project_id", project_id);
        params.put("region_id", region_id);
        params.put("name", name);
        String url = host + ADD_MUSIC;

        Map<String, File> files = new HashMap<>(1);
        files.put("file", file);

        String response = HttpUtil.uploadFile(url, params, files);
        logger.info("add music :{}", response);
    }

    /**
     * 音源列表
     *
     * @param userid
     * @param project_id
     * @param region_id
     * @param music_name
     * @return
     */
    public List<MusicData> searchMusic(String userid, String project_id, String region_id, String music_name) {
        Map<String, String> params = new HashMap<>(4);
        params.put("userid", userid);
        params.put("project_id", project_id);
        params.put("region_id", region_id);
        params.put("music_name", music_name);
        String url = host + SEARCH_MUSIC;

        String response = HttpUtil.doPost(url, params);
        MusicResult result = JSONUtil.parseObject(response, MusicResult.class);
        if (result != null && SUCESS.equals(result.getCode())) {
            return result.getDatas();
        }
        return null;
    }

    /**
     * 获取广播设备列表
     *
     * @param userid
     * @param project_id
     * @param region_id
     * @return
     */
    public List<DeviceData> readDeviceAll(String userid, String project_id, String region_id) {
        Map<String, String> params = new HashMap<>(3);
        params.put("userid", userid);
        params.put("project_id", project_id);
        params.put("region_id", region_id);
        String url = host + READ_DEVICE_ALL;
        String response = HttpUtil.doPost(url, params);
        DeviceResult result = JSONUtil.parseObject(response, DeviceResult.class);
        if (result != null && SUCESS.equals(result.getCode())) {
            return result.getDatas();
        }
        return null;
    }
}
